package com.dxwan.rxfit.ui.fragments;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.dxwan.rxfit.R;
import com.dxwan.rxfit.currency.models.CurrencyItem;

import java.util.List;

/**
 * {@link RecyclerView.Adapter} that can display a {@link CurrencyItem} and makes a call to the
 * specified {@link CurrencyFragment.OnListFragmentInteractionListener}.
 * TODO: Replace the implementation with code for your data type.
 */
public class MyCurrencyRecyclerViewAdapter extends RecyclerView.Adapter<MyCurrencyRecyclerViewAdapter.ViewHolder> {

    private final List<CurrencyItem> mValues;
    private final CurrencyFragment.OnListFragmentInteractionListener mListener;

    public MyCurrencyRecyclerViewAdapter(List<CurrencyItem> items, CurrencyFragment.OnListFragmentInteractionListener listener) {
        mValues = items;
        mListener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.fragment_currency, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {

        holder.mItem = mValues.get(position);
        //holder.mIdView.setText(mValues.get(position).id);
        //holder.mMidView.setText(mValues.get(position).content);
        holder.mIdView.setText(holder.mItem.CurrencyName);
        holder.mMidView.setText(String.format("%.02f", holder.mItem.MdlPrc));
        holder.mBidFxView.setText(String.format("%.02f", holder.mItem.BuyPrc));
        holder.mBidCashView.setText(String.format("%.02f", holder.mItem.CashBuyPrc));
        holder.mOfferView.setText(String.format("%.02f", holder.mItem.SellPrc));
        holder.mTimeView.setText(holder.mItem.CREATE_DATE);

        holder.mView.setOnClickListener(v -> {
            if (null != mListener) {
                // Notify the active callbacks interface (the activity, if the
                // fragment is attached to one) that an item has been selected.
                mListener.onListFragmentInteraction(holder.mItem);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mValues.size();
    }


    public void updateData(List<CurrencyItem> items) {
        mValues.clear();
        mValues.addAll(items);
        notifyDataSetChanged();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public final View mView;
        public final TextView mIdView;
        public final TextView mMidView;
        public final TextView mBidFxView;
        public final TextView mBidCashView;
        public final TextView mOfferView;
        public final TextView mTimeView;

        public CurrencyItem mItem;

        public ViewHolder(View view) {
            super(view);
            mView = view;
            mIdView = view.findViewById(R.id.id);
            mMidView = view.findViewById(R.id.mid);
            mBidFxView = view.findViewById(R.id.bid_fx);
            mBidCashView = view.findViewById(R.id.bid_cash);
            mOfferView = view.findViewById(R.id.bid_offer);
            mTimeView = view.findViewById(R.id.time);
        }

        @Override
        public String toString() {
            return super.toString() + " '" + mMidView.getText() + "'";
        }
    }
}
